home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / BASE_QUE.H < prev    next >
C/C++ Source or Header  |  1992-09-29  |  7KB  |  179 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MBN 08/29/89 -- Initial design and implementation
  13. // Updated: MBN 10/11/89 -- Changed "current_position" to "curpos" 
  14. // Updated: MBN 10/12/89 -- Added current_position() method for Iterator<Type>
  15. // Updated: LGO 02/02/90 -- Re-wrote practically everything
  16. // Updated: VDN 02/21/92 -- New lite version
  17. // Updated: JAM 09/23/92 -- removed DOS specifics, stdized #includes
  18. // Updated: JAM 09/23/92 -- renamed from CoolQueue to CoolBase_Queue
  19. // Updated: JAM 09/23/92 -- made *_state typedef a nested typedef "IterState"
  20. //                          as per new Iterator convention
  21. // Updated: JAM 09/29/92 -- index/size/pos type changed from [u]long to [u]int
  22. //
  23. // The  CoolQueue class is  publicly derived from the Generic  class and is used to
  24. // implement non-type specific functionality for the parameterized CoolQueue class.
  25. // In this manner, code  common  to all instances  of  the  CoolQueue class  can be
  26. // shared to  reduce code replication.  The CoolQueue  class implements  a circular
  27. // buffer of  a user-specified   type.   This is  accomplished by   using   the
  28. // parameterized  type capability of  C++. The CoolQueue will  grow  dynamically as
  29. // necessary with the amount of growth determined by the value of an allocation
  30. // size slot. Fixed  length queues are also  supported by setting  the value of
  31. // the allocation size slot to INVALID.
  32. //
  33. // The (Base) CoolQueue object contains in, out and limit indices into data
  34. // which is in the CoolQueue<Type> object.
  35. // If in == out, the buffer is empty.
  36. // If in > out, the area from out to in-1 contains available data.
  37. // If out > in, the area from out to limit-1 contains the first part of
  38. // the available data, and the area from first to in-1 contains the rest.
  39. // Note that if the buffer was completely full, in would equal out. We
  40. // we never let this happen, to preserve a simple empty check.
  41. //
  42. // There are  three  constructors  for the CoolQueue class.   The first constructor
  43. // takes no arguments and creates an empty  CoolQueue object of the specified type.
  44. // The second constructor takes an argument specifying the  initial size of the
  45. // CoolQueue.  Finally, the third constructor takes a single argument consisting of
  46. // a reference to a CoolQueue and duplicates its size and values.
  47. //
  48. // The CoolQueue class provides generic, type-independent methods to reset, move to
  49. // the  next, and   move to the  previous  element   via  the current  position
  50. // mechanism.  Methods to report  the number of  items in the queue,  check the
  51. // empty status,  and  clear all items from the  queue are  also provided.  The
  52. // assignment operator  is overloaded and three methods   to set the allocation
  53. // growth size, growth raito, and length (ie.   largest valid index  for random
  54. // access) of  the queue are  available. Finally,  exception handling functions
  55. // called by the  parameterized CoolQueue class are  located in  the  base class to
  56. // facilitate code sharing of common functionality.
  57. //
  58.  
  59. #ifndef BASE_QUEUEH                // If no class definition
  60. #define BASE_QUEUEH                // Indiciate we have done it
  61.  
  62. #include <iostream.h>        // include the Stream class header file
  63.  
  64. #ifndef MISCELANEOUSH        // If we have not included this file,
  65. #include <misc.h>        // include miscelaneous useful definitions.
  66. #endif
  67.  
  68. #define QUEUE_MEM_BLK_SZ 100
  69.  
  70. class CoolBase_Queue {
  71. protected:
  72.   int in;                    // 1+ last element inserted
  73.   int out;                    // next element to remove
  74.   int limit;                    // Size of allocated storage
  75.   int curpos;                    // Keeps current position
  76.   int alloc_size;                // Allocation size for growth
  77.   static float growth_ratio_s;            // If non-zero, growth ratio 
  78.  
  79.   void value_error (const char*);        // Raise exception 
  80.   void look_error (const char*);        // Raise exception
  81.   void resize_error (const char*);        // Raise exception
  82.   void assign_error (const char*);        // Raise exception
  83.   
  84. public:
  85.   typedef int IterState;            // Current position state
  86.  
  87.   CoolBase_Queue ();                    // CoolBase_Queue q;
  88.   CoolBase_Queue (int);                    // CoolBase_Queue q(10);
  89.   CoolBase_Queue (const CoolBase_Queue&);                // CoolBase_Queue q = q2;
  90.   ~CoolBase_Queue ();                    // CoolBase_Queue destructor
  91.  
  92.   inline void reset ();                // Invalidate current position
  93.   inline Boolean next ();            // Advance current position
  94.   inline Boolean prev ();            // Backup current position 
  95.   
  96.   inline Boolean is_empty() const;        // Is queue empty ?
  97.   inline void clear();                // Empty queue
  98.   inline int capacity() const;            // Max. number of elements
  99.   inline IterState& current_position ();    // Set/get current position
  100.  
  101.   int length() const;                // Number of items in queue
  102.   void set_growth_ratio (float, const char*);    // Set growth percentage
  103.   void set_alloc_size (int, const char*);    // Set alloc size
  104. };
  105.  
  106.  
  107. // capacity -- Return maximum number of elements object can hold
  108. // Input:      None
  109. // Output:     Integer value of maximum number of elements
  110.  
  111. inline int CoolBase_Queue::capacity () const {
  112.   return (this->limit);
  113. }
  114.  
  115.  
  116. // Boolean is_empty() -- Return TRUE if this CoolBase_Queue is empty
  117. // Input:                None
  118. // Output:               TRUE or FALSE
  119.  
  120. inline Boolean CoolBase_Queue::is_empty () const {
  121.   return in == out;
  122. }
  123.  
  124.  
  125. // void reset () -- Invalidate current position
  126. // Input:           None
  127. // Output:          None
  128.  
  129. inline void CoolBase_Queue::reset () {
  130.   this->curpos = in;
  131. }
  132.  
  133.  
  134. // Boolean next () -- Return TRUE if able to advance current position by one;
  135. //                    advance in this case means towards the front of the CoolBase_Queue
  136. // Input:             None
  137. // Output:            TRUE or FALSE
  138.  
  139. inline Boolean CoolBase_Queue::next () {    
  140.   if (curpos == in)
  141.     curpos = out;
  142.   else if (++curpos >= limit)
  143.     curpos = 0;        // increment and wrap
  144.   return curpos != in;
  145. }
  146.  
  147.  
  148. // Boolean prev () -- Return TRUE if able to backup current position by one;
  149. //                    backup means going towards the back of the CoolBase_Queue
  150. // Input:             None
  151. // Output:            TRUE or FALSE
  152.  
  153. inline Boolean CoolBase_Queue::prev() {
  154.   if (--curpos < 0)
  155.     curpos = limit - 1;    // decrement and wrap
  156.   return curpos != in;  
  157. }
  158.  
  159.  
  160. // void clear() -- Empty this CoolBase_Queue  
  161. // Input:  None
  162. // Output: None
  163.  
  164. inline void CoolBase_Queue::clear () {
  165.   curpos = in = out = 0;
  166. }
  167.  
  168.  
  169. // current_position () -- Return current position state
  170. // Input:                 None
  171. // Output:                Reference to current position state
  172.  
  173. inline CoolBase_Queue::IterState& CoolBase_Queue::current_position () {
  174.   return this->curpos;
  175. }
  176.  
  177. #endif                        // End of BASE_QUEUEH
  178.  
  179.